package edu.dayu.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import edu.dayu.mapper.ApplicantMapper;
import edu.dayu.mapper.ClubPersonMapper;
import edu.dayu.pojo.Applicant;
import edu.dayu.pojo.ClubPerson;
import edu.dayu.service.ApplicantService;
import edu.dayu.util.ClubException;
import edu.dayu.util.ResultCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import java.util.Date;
import java.util.List;

@Service
public class ApplicantServiceImpl implements ApplicantService {
    @Autowired
    private ApplicantMapper applicantMapper;

    @Autowired
    private ClubPersonMapper clubPersonMapper;

    @Autowired
    private JavaMailSenderImpl javaMailSender;

    @Value("${spring.mail.username}")
    private String sendMailer;

    @Override
    public PageInfo<Applicant> applicantList(Applicant applicant) {
        PageHelper.startPage(applicant.getPageNum(),applicant.getPageSize());
        List<Applicant> applicants = applicantMapper.applicantList(applicant);
        PageInfo<Applicant> applicantPageInfo = new PageInfo<>(applicants);
        return applicantPageInfo;
    }

    @Override
    public boolean applicantInsert(Applicant applicant) {
        ClubPerson clubPerson = new ClubPerson();
        clubPerson.setName(applicant.getName());
        Applicant applicant1 = new Applicant();
        applicant1.setName(applicant.getName());
        List<Applicant> applicants = applicantMapper.applicantList(applicant1);
        if (applicants.size() == 0) {
            List<ClubPerson> clubPeople = clubPersonMapper.clubPersonList(clubPerson);
            if (clubPeople.size() == 0) {
                applicant.setCreateTime(new Date());
                applicantMapper.applicantInsert(applicant);
                return true;
            }
        }
        return false;
    }

    @Override
    public void applicantDelete(Applicant applicant) {
        applicantMapper.applicantDelete(applicant);
    }

    @Override
    public void applicantPass(Applicant applicant) {
        ClubPerson clubPerson = new ClubPerson();
        clubPerson.setName(applicant.getName());
        clubPerson.setEmail(applicant.getEmail());
        clubPerson.setC_id(applicant.getC_id());
        clubPerson.setPosition("社员");
        clubPersonMapper.clubPersonInsert(clubPerson);
        applicantMapper.applicantDelete(applicant);
    }

    @Async
    public void sendMail(String email , String text) {
        if (email ==null || "".equals(email) )
            throw new ClubException(ResultCodeEnum.PARAM_ERROR);
        try {
            //true 代表支持复杂的类型
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(javaMailSender.createMimeMessage(),false);
            mimeMessageHelper.setFrom(sendMailer);
            mimeMessageHelper.setTo(email);
            mimeMessageHelper.setText(text);
            mimeMessageHelper.setSubject("社团通知");
            //邮件发送时间
            mimeMessageHelper.setSentDate(new Date());
            //发送邮件
            javaMailSender.send(mimeMessageHelper.getMimeMessage());
        } catch (MessagingException e) {
            e.printStackTrace();
        }

    }
}
